#coding=utf-8
import os
import tsina

os.environ['DJANGO_SETTINGS_MODULE'] = 'coolie.settings'

from django.db import IntegrityError
from hellojayproxy.hjp.models import TsinaUser

keywords = ['twitter', 'facebook', '翻%20墙']
search_url = 'http://t.sina.com.cn/k/%s'

t = tsina.TsinaRobot('twinsjp@sina.cn', 'jayproxy')
t.login()

for keyword in keywords:
    print keyword
    url = search_url%keyword
    a,b, h = t._get_url(url)
    ms = tsina.parse_search(h)
    ms.reverse()

    for m in ms:
        c = TsinaUser(user_id=m.user_id, nick_name=m.user_nick_name, message=m.content,\
                      published_at=m.published_at, client=m.client)
        try:
            c.save()
            print c.nick_name
        except IntegrityError,e:
            continue
